package com.lin.thread.task;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.BufferedMutator;
import org.apache.hadoop.hbase.client.BufferedMutatorParams;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;


public class InHbaseByMutatorTask implements Runnable{
    private List<Put> puts;
    private Connection connection;
    // 线程池任务计数器
    private CountDownLatch countDownLatch;

    public InHbaseByMutatorTask(List<Put> puts, Connection connection) {
        this.puts = puts;
        this.connection = connection;
    }

    public InHbaseByMutatorTask(List<Put> puts, Connection connection, CountDownLatch countDownLatch) {
        this.puts = puts;
        this.connection = connection;
        this.countDownLatch = countDownLatch;
    }

    public InHbaseByMutatorTask() {
    }

    @Override
    public void run() {
        long s = System.currentTimeMillis();
        BufferedMutatorParams params = new BufferedMutatorParams(TableName.valueOf("MOMO_CHAT:MSG"));
        params.writeBufferSize(4 * 1024 * 1024);
        BufferedMutator mutator = null;
        try {
            mutator = connection.getBufferedMutator(params);
            mutator.mutate(puts);
            mutator.flush();
            mutator.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        countDownLatch.countDown();
        long e = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "==" + puts.size() + "耗时：" + (e - s));
    }
}
